<template>
  <div class="inventory-dialog">
    <el-dialog :visible.sync="dialogFormVisible" :class="{ 'mobile-el-dialog' : isMobile}" :before-close="handleClose" class="small-common-dialog-size">
      <Title title="导入成本时间设置" font-size="16px" style="margin-bottom: 24px;"/>
      <el-form ref="projectForm" :model="projectForm" :rules="projectRules" label-width="115px" label-position="left">
        <el-form-item label="生效开始时间" prop="effectTime">
          <el-date-picker v-model="projectForm.effectTime" :clearable="false" :editable="false" type="date" size="small" value-format="timestamp" format="yyyy/MM/dd" placeholder="选择日期" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button class="cancel-btn-style" type="info" @click="handleClose">取 消</el-button>
        <!-- <el-button type="primary" @click="addInventory">上传文件</el-button> -->
        <!-- :extend-params="{url: ''}" -->
        <ImportButton
          :import-api="'/admin/youzan/supply/import'"
          :show-icon="false"
          :effect-time="projectForm.effectTime"
          :get-list="handleSuccess"
          accept=".csv,.xls,.xlsx"
          class="upload-btn ok"
          @onSucess="handleSuccess"
          @closeAdd="handleClose"
          @error501="reUpload"/>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import ImportButton from './ImportBtn';
import { postReUpload } from '@/api/commerce';
import Title from '@/components/Title';

export default {
  components: {
    ImportButton,
    Title
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    isMobile: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      projectForm: {
        effectTime: new Date(new Date().toLocaleDateString()).getTime() // 当天0点
      },
      projectRules: {
        effectTime: [{ required: true, trigger: 'blur', message: '请选择生效开始时间' }]
      }
    };
  },
  created() {
  },
  methods: {
    reUpload(params) {
      postReUpload(params).then(res => {
        this.$message.success('上传成功');
        this.handleSuccess();
      }).catch(err => {
        this.$message.error(err.message);
      });
    },
    handleClose() {
      this.resetForm();
      this.$emit('close');
    },
    resetForm() {
      this.projectForm = {
        effectTime: new Date(new Date().toLocaleDateString()).getTime() // 当天0点
      };
    },
    handleSuccess() {
      this.$emit('success');
    }
  }
};
</script>
<style lang="scss" scoped>
.inventory-dialog {
  ::v-deep {
    .select-component-withouthttp {
      width: 100%;
    }
  }
}
.upload-btn {
  display: inline-block;
}
</style>
